﻿namespace Blaze.Editor
{
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using UnityEditor;
    using UnityEngine;

    public class BlazeEditorUtil
    {
        #region Header and contents

        public static void BeginContents()
        {
            EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(10f));
            GUILayout.Space(10f);
            GUILayout.BeginVertical();
            GUILayout.Space(2f);
        }

        public static void EndContents()
        {
            GUILayout.Space(3f);
            GUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(3f);
        }

        public static bool DrawHeader(string text, Color color)
        {
            return DrawHeader(text, text, false, false, color);
        }

        public static bool DrawHeader(string text)
        {
            return DrawHeader(text, text, false, false, Color.white);
        }

        public static bool DrawHeader(string text, string key, bool forceOn, bool minimalistic, Color color)
        {
            bool state = false;
            if (ProjectEditorPrefs.HasKey(key))
            {
                state = ProjectEditorPrefs.GetBool(key, true);
            }

            GUILayout.Space(3f);
            if (!forceOn && !state) GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
            GUILayout.BeginHorizontal();
            GUI.changed = false;

            if (minimalistic)
            {
                if (state) text = "\u25BC" + (char) 0x200a + text;
                else text = "\u25BA" + (char) 0x200a + text;

                GUILayout.BeginHorizontal();
                GUI.contentColor = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.7f) : new Color(0f, 0f, 0f, 0.7f);
                if (!GUILayout.Toggle(true, text, "PreToolbar2", GUILayout.MinWidth(20f))) state = !state;
                GUI.contentColor = Color.white;
                GUILayout.EndHorizontal();
            }
            else
            {
                text = "<b><size=11>" + text + "</size></b>";
                if (state) text = "\u25BC " + text;
                else text = "\u25BA " + text;
                GUI.contentColor = color;
                if (!GUILayout.Toggle(true, text, "dragtab", GUILayout.MinWidth(20f))) state = !state;
                GUI.contentColor = Color.white;
            }

            if (GUI.changed) ProjectEditorPrefs.SetBool(key, state);

            if (!minimalistic) GUILayout.Space(2f);
            GUILayout.EndHorizontal();
            GUI.backgroundColor = Color.white;
            if (!forceOn && !state) GUILayout.Space(3f);
            return state;
        }

        #endregion

        #region Table views

        public static readonly Color TitleColor = new Color32(38, 158, 111, 255); // basically green
        public static readonly Color TitleColorSelected = new Color32(19, 80, 60, 255); // dark green

        public static readonly Color SelectionColor = new Color32(62, 95, 150, 255);
        public static readonly Color SelectionColorDark = new Color32(62, 95, 150, 128);
        public static readonly string BytesFormatter = "<fmt_bytes>";

        public static object FieldValue(object obj, FieldInfo fieldInfo)
        {
            if (obj == null)
                return "";
            if (fieldInfo == null)
                return "";

            return fieldInfo.GetValue(obj);
        }

        public static string FieldToString(object obj, FieldInfo fieldInfo, string fmt)
        {
            var val = FieldValue(obj, fieldInfo);
            if (val == null)
                return "";

            if (fmt == BytesFormatter)
                return EditorUtility.FormatBytes((int) val);
            if (val is float)
                return ((float) val).ToString(fmt);
            if (val is double)
                return ((double) val).ToString(fmt);
            return val.ToString();
        }

        public static string GetRandomString()
        {
            string path = Path.GetRandomFileName();
            path = path.Replace(".", ""); // Remove period.
            return path;
        }

        public static Texture2D GetColorTexture(Color c)
        {
            Texture2D tex;
            if (mColorTextures.TryGetValue(c, out tex))
                return tex;

            tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
            tex.SetPixel(0, 0, c);
            tex.Apply();

            mColorTextures[c] = tex;
            return tex;
        }

        public static void DrawLabel(string content, GUIStyle style)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            EditorGUILayout.LabelField(content, style, GUILayout.Width(style.CalcSize(new GUIContent(content)).x + 3));
            EditorGUILayout.EndHorizontal();
        }

        private static readonly Dictionary<Color, Texture2D> mColorTextures = new Dictionary<Color, Texture2D>();

        #endregion

        #region File id

        /// <summary>
        /// 获取指定对象的Unity内置fileid属性。
        /// </summary>     
        public static long GetFileId(Object target)
        {
            var serializedObject = new SerializedObject(target);
            mInspectorMode.SetValue(serializedObject, InspectorMode.Debug, null);
            var localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
            return localIdProp.longValue;
        }

        private static readonly PropertyInfo mInspectorMode = typeof (SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);

        #endregion
    }
}